package com.atguigu.gmall;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * @author: atguigu
 * @create: 2023-03-01 09:07
 */
public class CompletableFutureReturnAccpectAsync {

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        //1.创建线程A异步操作对象-需求线程A有返回结果
        CompletableFuture<String> futureA = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t线程A执行了");
            return "AAA";
        });

        //2.基于线程A异步操作对象 构建线程B的异步操作对象 需求:B线程需要A线程返回结果,但是B线程没有结果返回

        CompletableFuture<Void> futureB = futureA.thenAcceptAsync((futureAResult -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(Thread.currentThread().getName() + "\t线程B执行了,获取到线程A结果:" + futureAResult);
        }));


        CompletableFuture.anyOf(futureA, futureB);

        System.out.println("主线程执行");
        //futureB.get();

    }
}
